#include "stdafx.h"
#include "ResponseHeader.h"
#include "HTMLUtility.h"

void CResponseHeader::setContentLength()
{
	size_t nStart, nEnd;

	if ((nStart = this->m_strResponseHeader.find(ContentLengthMark)) != string::npos)
	{
		nEnd = m_strResponseHeader.find(LineBoundary, nStart);
		if (nEnd != string::npos)
		{
			string &s = m_strResponseHeader.substr(nStart + strlen(ContentLengthMark), nEnd - nStart - strlen(ContentLengthMark));
			//"Content-Length: 135381"
			this->m_resHeader.iResponseContentLength = atoi(s.c_str());
		}
	}
	else
		this->m_resHeader.iResponseContentLength = -1;
}

void CResponseHeader::CheckIsHtml()
{
	bool bResult = false;
	if (m_strResponseHeader.find(szStatusCode) == string::npos)
	{
		bResult = false;
	}
	else if (m_strResponseHeader.find(szContentType) == string::npos)
	{
		bResult = false;
	}
	else
		bResult = true;

	this->m_resHeader.bHTML = bResult;
}

void CResponseHeader::CheckIsChunked()
{
	const char * szTransferEncoding = "Transfer-Encoding: chunked\r\n";
	this->m_resHeader.bChunked = this->m_strResponseHeader.find(szTransferEncoding) != string::npos ? TRUE : FALSE;
}




CResponseHeader::CResponseHeader()
{
}


CResponseHeader::~CResponseHeader()
{
}

void CResponseHeader::setResponseHeader(const string& szData)
{
	this->m_resHeader.iResponseHeaderLength = szData.find(EndBoundary) + 4;

	m_strResponseHeader.assign(szData, 0, this->m_resHeader.iResponseHeaderLength);
}

bool CResponseHeader::IsHeaderCompleted(const char * szData)
{
	if (strstr(szData, EndBoundary)) {
		return true;
	}

	return false;
}

void CResponseHeader::ParseResponse()
{
	setContentLength();

	CheckIsHtml();

	CheckIsChunked();


}

bool CResponseHeader::IsHtml()
{
	return this->m_resHeader.bHTML;
}

const RESPONSEHEADER & CResponseHeader::getHeader() const
{
	return m_resHeader;
}

int CResponseHeader::getLength()
{
	return this->m_resHeader.iResponseHeaderLength + this->m_resHeader.iResponseContentLength;
}

bool CResponseHeader::HasContentLength()const
{
	return this->m_resHeader.iResponseContentLength == -1 ? false : true;
}

int CResponseHeader::getContentLength()const
{
	return this->m_resHeader.iResponseContentLength;
}

int CResponseHeader::getHeaderLength() const
{
	return this->m_resHeader.iResponseHeaderLength;
}

const std::string & CResponseHeader::getResponseHeaderString() const
{
	return m_strResponseHeader;
}

void CResponseHeader::ModifyContentLength(int nLen)
{
	m_resHeader.iResponseContentLength = nLen;
	char szContentLength[64] = { 0 };
	sprintf_s(szContentLength, "Content-Length: %d", nLen);
	
	//Conteng-Length: xxx
	CHTMLUtility::ReplaceLine(m_strResponseHeader, "Content-Length: ", szContentLength);
	
	setContentLength();
}

void CResponseHeader::ModifyContent_Encoding_GZIP_none()
{
	//Content-Encoding: gzip
	CHTMLUtility::ReplaceLine(m_strResponseHeader, "Content-Encoding: ", "Content-Encoding: none");
}

void CResponseHeader::ModifyTransferEncoding_Chunked_none()
{
	CHTMLUtility::ReplaceLine(m_strResponseHeader, "Transfer-Encoding: ", "Transfer-Encoding: none");
}
